package com.sxdx.interceptor;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.sxdx.entity.Teacher;

@SuppressWarnings("all")
public class AuthenticationInterceptor extends MethodFilterInterceptor{
	private static Logger logger = LogManager.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request =  ServletActionContext.getRequest();
		String local = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ServletActionContext.getRequest().getRequestURI();
		logger.trace(local+Thread.currentThread().getStackTrace()[1].getMethodName());
		ActionContext context = invocation.getInvocationContext();
		Map<String, Object> sessionValues = context.getSession();
		Teacher teacher = (Teacher) sessionValues.get("teacher");
		String method = invocation.getProxy().getMethod();
		//		System.out.println("Action："+invocation.getAction().getClass().getName());  
		//		System.out.println("Struts2 中配置的Action："+invocation.getProxy().getActionName());  
		//		System.out.println("调用的方法："+invocation.getProxy().getMethod()); 
		if (teacher == null) {
			return Action.LOGIN;
		}else{
			return invocation.invoke();  
		}
	}
}
